<!--  -->
<template>
  <div class='app-container'>
    <el-col :span="24" :xs="24">
      <el-form :model="tableParams" ref="queryForm" size="small" :inline="true" v-show="true" label-width="100px">
        <el-form-item label="课程分类编码" prop="code">
          <el-input v-model="tableParams.code" placeholder="请输入课程分类编码" clearable style="width: 240px"
            @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item label="课程分类名称" prop="name">
          <el-input v-model="tableParams.name" placeholder="请输入课程分类名称" clearable style="width: 240px"
            @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="getList">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>

      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
            v-hasPermi="['system:user:add']">新增</el-button>
        </el-col>
        <!-- <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" :columns="columns"></right-toolbar> -->
      </el-row>
      <my-table :loading="loading" rowKey="id" :tableData="tableData" :paging="false" :params="tableParams"
        :treeProps="treeProps">
        <!-- <el-table-column type="index" width="50" label="#" align="center"></el-table-column> -->
        <el-table-column prop="code" label="课程分类类别" align="center">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.type == 0" type="success">普通分类</el-tag>
            <el-tag v-else-if="scope.row.type == 1" type="danger">专题</el-tag>
            <el-tag v-else-if="scope.row.type == 2" type="danger">外部链接</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="code" label="课程分类编码" align="center"></el-table-column>
        <el-table-column prop="name" label="课程分类名称" align="center"></el-table-column>
        <el-table-column prop="status" label="状态" align="center">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.isDisabled == 0" type="success">启用</el-tag>
            <el-tag v-else-if="scope.row.isDisabled == 1" type="danger">禁用</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="180" align="center">
          <template slot-scope="scope">
            <el-button type="text" size="mini" @click="handleEdit(scope.row)"
              v-hasPermi="['system:user:edit']">编辑</el-button>
            <el-button type="text" size="mini" @click="handleDelete(scope.row)"
              v-hasPermi="['system:user:remove']">删除</el-button>
          </template>
        </el-table-column>
      </my-table>
    </el-col>
    <!-- 课程分类弹窗 -->
    <el-dialog :title="dialogTitle" :visible.sync="dialogFormVisible" width="600px" append-to-body
      :close-on-click-modal="false" :close-on-press-escape="false">
      <el-form :model="dialogForm" :rules="rules" ref="dialogForm" size="small" label-width="120px">
        <el-form-item label="类型" prop="type">
          <el-radio-group v-model="dialogForm.type" :disabled="dialogForm.id">
            <el-radio :label="0">普通分类</el-radio>
            <el-radio :label="1">专题</el-radio>
            <el-radio :label="2">外部链接</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="类型图片" prop="photoUrl">
          <imageUpload v-model="dialogForm.photoUrl" :value="dialogForm.photoUrl" :limit="1"></imageUpload>
        </el-form-item>
        <template v-if="dialogForm.type == 0 || dialogForm.type == 1">
          <el-form-item label="上级分类" prop="pid">
            <el-select v-model="dialogForm.pid">
              <el-option v-for="item in options" :key="item.id" :label="item.name" :value="item.id"></el-option>
            </el-select>
            <!-- <el-cascader v-model="dialogForm.pid" placeholder="请选择上级分类" clearable :show-all-levels="false"
            :options="options" :props="{ children: 'list', value: 'id', label: 'name', checkStrictly: true }"></el-cascader> -->
          </el-form-item>
          <el-form-item label="课程分类编码" prop="code">
            <el-input v-model="dialogForm.code" :disabled="dialogForm.id" placeholder="请输入课程分类编码" clearable style="width: 240px" />
          </el-form-item>
          <el-form-item label="课程分类名称" prop="name">
            <el-input v-model="dialogForm.name" maxlength="10" placeholder="请输入课程分类名称" clearable style="width: 240px" />
          </el-form-item>
        </template>
        <template v-else>
          <el-form-item label="第三方链接" prop="remark">
            <el-input v-model="dialogForm.remark" placeholder="请输入第三方链接" clearable style="width: 240px" />
          </el-form-item>
        </template>
        <el-form-item label="排序">
          <el-input-number v-model="dialogForm.orderNum" :min="0" :max="9999" style="width: 240px"></el-input-number>
        </el-form-item>
        <el-form-item label="状态" prop="isDisabled">
          <el-radio-group v-model="dialogForm.isDisabled">
            <el-radio :label="0">启用</el-radio>
            <el-radio :label="1">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
//这里可以导入其他文件（比如：组件，工具js，第三方插件js，json文件，图片文件等等）
//例如：import 《组件名称》 from '《组件路径》';
import { getCourseTypeList, addCourseType, updateCourseType, deleteCourseType } from '@/api/courseManage/courseType';
import { getTreeIds } from '@/utils/utils';
export default {
  //import引入的组件需要注入到对象中才能使用
  components: {},
  data() {
    //这里存放数据
    return {
      loading: false,
      tableData: [],//表格数据
      tableParams: {
        code: "",
        name: "",
      },//表格参数
      treeProps: {
        children: 'list',
      },
      dialogFormVisible: false,//弹窗显示隐藏
      dialogForm: {
        type: 0,
        photoUrl: "",  //图片
        pid: "",
        code: "",
        name: "",
        isDisabled: 0,
        remark:"",
        orderNum:"",
      },//弹窗数据
      options: [], //级联选择数据
      dialogTitle: '新增课程分类',
      rules: {
        code: [
          { required: true, message: '请输入课程分类编码', trigger: 'blur' }
        ],
        name: [
          { required: true, message: '请输入课程分类名称', trigger: 'blur' }
        ],
        status: [
          { required: true, message: '请选择状态', trigger: 'change' }
        ],
        remark: [
          { required: true, message: '请输入第三方链接', trigger: 'blur' }
        ],
      },
      submiting: false,
    };
  },
  //监听属性 类似于data概念
  computed: {},
  //监控data中的数据变化
  watch: {},
  //方法集合
  methods: {
    //获取表格数据
    getList() {
      this.loading = true;
      getCourseTypeList(this.tableParams).then(res => {
        this.loading = false;
        if (res.code == 200) {
          this.tableData = res.data;
        } else {
          this.$message.error(res.msg)
        }
      }).catch(err => {
        this.loading = false;
      })
    },
    // 获取上级选项数据
    getOptions() {
      getCourseTypeList({ pId: 0 }).then(res => {
        if (res.code == 200) {
          this.options = res.data;
        } else {
          this.$message.error(res.msg)
        }
      })
    },
    // 重置
    resetQuery() {
      this.tableData = [];
      this.tableParams.code = "";
      this.tableParams.name = "";
      this.getList();
    },
    // 添加
    handleAdd() {
      this.dialogFormVisible = true;
      this.dialogTitle = '新增课程分类';
      this.dialogForm = {
        type: 0,
        pid: "",
        photoUrl: "",  //图片
        code: "",
        name: "",
        isDisabled: 0,
        remark:"",
        orderNum:"",
      }
      this.$nextTick(() => {
        this.$refs['dialogForm'].clearValidate();
      })
    },
    //修改
    handleEdit(row) {
      console.log(row);
      this.dialogFormVisible = true;
      this.dialogTitle = '修改课程分类';
      // let pid = getTreeIds(this.tableData,row.pid,{children:'list',id:'id'})['ids'];
      this.dialogForm = {
        pid: row.pid,
        type: row.type,
        code: row.code,
        name: row.name,
        isDisabled: row.isDisabled,
        id: row.id,
        photoUrl: row.photoUrl,
        remark:row.remark,
        orderNum:row.orderNum,
      }
    },
    //提交
    submitForm() {
      console.log(this.dialogForm);
      this.$refs['dialogForm'].validate((valid) => {
        if (valid) {
          this.submiting = true;
          let params = {
            // pid: this.dialogForm.pid.length > 0 ? this.dialogForm.pid[this.dialogForm.pid.length - 1] : '',
            pid: this.dialogForm.pid,
            type: this.dialogForm.type,
            code: this.dialogForm.code,
            name: this.dialogForm.name,
            isDisabled: this.dialogForm.isDisabled,
            id: this.dialogForm.id ? this.dialogForm.id : '',
            photoUrl: this.dialogForm.photoUrl,
            remark:this.dialogForm.remark,
            orderNum:this.dialogForm.orderNum,
          }
          if (this.dialogTitle == '新增课程分类') {
            addCourseType(params).then(res => {
              this.submiting = false;
              if (res.code == 200) {
                this.$message.success(res.msg);
                this.dialogFormVisible = false;
                this.getList();
              } else {
                this.$message.error(res.msg)
              }
            })
          } else {
            updateCourseType(params).then(res => {
              this.submiting = false;
              if (res.code == 200) {
                this.$message.success(res.msg);
                this.dialogFormVisible = false;
                this.getList();
              } else {
                this.$message.error(res.msg)
              }
            })
          }
        } else {
          return false;
        }
      });
    },
    // 删除
    handleDelete(row) {
      this.$confirm('是否确认删除该课程分类？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteCourseType(row.id).then(res => {
          console.log(res);
          if (res.code == 200) {
            this.$message.success(res.msg);
            this.getList();
          } else {
            this.$message.error(res.msg)
          }
        }).catch(err => {
          console.log(err);
        })
      }).catch(() => {
        this.$message.info('已取消删除');
      });
    },
    cancel() {
      this.dialogFormVisible = false;
    }
  },
  //生命周期 - 创建完成（可以访问当前this实例）
  created() {

  },
  //生命周期 - 挂载完成（可以访问DOM元素）
  mounted() {
    this.getList();
    this.getOptions();
  },
  beforeCreate() { }, //生命周期 - 创建之前
  beforeMount() { }, //生命周期 - 挂载之前
}
</script>
<style scoped></style>
